package user

import (
	"github.com/gin-gonic/gin"
	"time"
	"zzqedu/controller/base"
	"zzqedu/form/user"
	form_user "zzqedu/form/user"
	"zzqedu/global"
	"zzqedu/model"
	"zzqedu/response"
)

/*
*
用户的列表,带关键字和日期返回搜索
*/
func List(c *gin.Context) {
	var userList []model.User
	var userListReq form_user.UserListRequest
	var count int64
	var userListResponse []form_user.UserListResponse
	var userListRes form_user.UserListRes
	if err := c.ShouldBindJSON(&userListReq); err != nil {
		response.FailWithMessage(c, "请求参数错误")
		return
	}
	localDb := global.DB.Model(&model.User{})
	if userListReq.Mobile != "" {
		localDb = localDb.Where("mobile like ? ", "%"+userListReq.Mobile+"%")
	}
	if userListReq.NickName != "" {
		localDb = localDb.Where("nick_name like ? ", "%"+userListReq.NickName+"%")
	}
	if userListReq.Gender != "" {
		localDb = localDb.Where("gender like ? ", "%"+userListReq.Gender+"%")
	}
	//生日的搜索
	if userListReq.BirthdayFrom != "" {
		localDb = localDb.Where("birthday >= ? ", userListReq.BirthdayFrom)
	}
	if userListReq.BirthdayTo != "" {
		localDb = localDb.Where("birthday <= ? ", userListReq.BirthdayTo)
	}
	//获取总数量
	localDb.Count(&count)
	if res := localDb.Scopes(base.Paginate(userListReq.Page, userListReq.PageSize)).Find(&userList); res.Error != nil {
		response.FailWithMessage(c, res.Error.Error())
		return
	}
	for _, user := range userList {
		userListResponse = append(userListResponse, form_user.UserListResponse{
			Mobile:   user.Mobile,
			NickName: user.NickName,
			Birthday: user.Birthday.Format("2006-01-02 15:04:05"),
			Gender:   user.Gender,
		})
	}
	userListRes.UserListResponse = userListResponse
	userListRes.Count = count
	response.SuccessWithDetail(c, "获取用户列表成功", userListRes)
}

/*
*
添加账户
*/
func Add(c *gin.Context) {
	var addUser user.AddUser
	if err := c.ShouldBindJSON(&addUser); err != nil {
		response.FailWithMessage(c, "参数校验错误")
		return
	}
	if result := global.DB.Where("mobile = ?", addUser.Mobile).First(&model.User{}); result.RowsAffected == 1 {
		response.FailWithMessage(c, "当前手机号的用户已经添加了")
		return
	}
	//对前端传递的birthday进行处理
	parse, _ := time.Parse("2006-01-02", addUser.Birthday)
	user := model.User{
		Mobile:   addUser.Mobile,
		Password: addUser.Password,
		NickName: addUser.NickName,
		Birthday: &parse,
		Gender:   addUser.Gender,
	}
	if result := global.DB.Save(&user); result.Error != nil {
		response.SuccessWithMessage(c, "添加用户失败")
		return
	} else {
		response.FailWithMessage(c, "添加用户成功")
	}
}

/*
*
添加账户
*/
func Detail(c *gin.Context) {
	id := c.Param("id")
	var user model.User
	var userDetail form_user.UserDetail
	if res := global.DB.First(&user, id); res.RowsAffected == 0 {
		response.FailWithMessage(c, "查找不到用户")
		return
	} else {
		userDetail = form_user.UserDetail{
			ID:        user.ID,
			Mobile:    user.Mobile,
			NickName:  user.NickName,
			Birthday:  user.Birthday.Format("2006-01-02 15:04:05"),
			Gender:    user.Gender,
			CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdatedAt: user.UpdatedAt.Format("2006-01-02 15:04:05"),
		}
		response.SuccessWithDetail(c, "返回用户详情成功", userDetail)
	}
}

/*
*
修改账户
*/
func Update(c *gin.Context) {
	id := c.Param("id")
	var req form_user.UpdateUser
	var user model.User
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(c, "参数绑定错误")
		return
	}
	result := global.DB.First(&user, id)
	if result.RowsAffected == 0 {
		response.FailWithMessage(c, "找不到当前用户")
		return
	}
	if req.Password != "" {
		user.Password = req.Password
	}
	if req.Gender != "" {
		user.Gender = req.Gender
	}
	if req.NickName != "" {
		user.NickName = req.NickName
	}
	if req.Birthday != "" {
		parse, _ := time.Parse("2006-01-02", req.Birthday)
		user.Birthday = &parse
	}
	res := global.DB.Save(&user)
	if res.Error != nil {
		response.FailWithMessage(c, "更新用户失败")
		return
	} else {
		response.SuccessWithMessage(c, "更新用户成功")
	}
}

/*
*
删除
*/
func Delete(c *gin.Context) {
	id := c.Param("id")
	user := global.DB.First(&model.User{}, id)
	if user.RowsAffected == 0 {
		response.FailWithMessage(c, "查找不到当前用户")
		return
	}
	if res := global.DB.Delete(&model.User{}, id); res.RowsAffected == 0 {
		response.FailWithMessage(c, "删除用户失败")
		return
	}
	response.SuccessWithMessage(c, "删除用户成功")
}

/*
*
测试接口
*/
func Test(c *gin.Context) {
	str := global.UserService.GetUserList()
	response.SuccessWithMessage(c, str)
}
